package com.ala4.oxcafe.service.impl;

import com.ala4.oxcafe.boot.page.PageDomain;
import com.ala4.oxcafe.common.utils.collection.CollUtil;
import com.ala4.oxcafe.common.utils.object.ObjectUtil;
import com.ala4.oxcafe.entity.FilmProduct;
import com.ala4.oxcafe.exception.BusinessException;
import com.ala4.oxcafe.repository.FilmProductRepository;
import com.ala4.oxcafe.service.FilmProductService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车膜产品服务类
 *
 * @author PING
 * @version 1.0.0
 * @date 2025/8/14 13:12
 */
@Service
@AllArgsConstructor
public class FilmProductServiceImpl implements FilmProductService {

    private final FilmProductRepository filmProductRepository;

    @Override
    public IPage<FilmProduct> filmProductPage(PageDomain pageDomain, FilmProduct filmProduct) {
        return filmProductRepository.selectFilmProductPage(pageDomain, filmProduct);
    }

    @Override
    public FilmProduct createFilmProduct(FilmProduct filmProduct) {
        List<FilmProduct> codeList = filmProductRepository.list(Wrappers.<FilmProduct>lambdaQuery().eq(FilmProduct::getCode, filmProduct.getCode()));
        if (CollUtil.isNotEmpty(codeList)) {
            throw new BusinessException("产品编码[{}]已存在,请重新输入", filmProduct.getCode());
        }
        filmProductRepository.save(filmProduct);
        return filmProduct;
    }

    @Override
    public FilmProduct updateFilmProduct(FilmProduct filmProduct) {
        filmProductRepository.updateById(filmProduct);
        return filmProduct;
    }

    @Override
    public void deleteFilmProduct(Long productId) {
        FilmProduct filmProduct = this.filmProductInfo(productId);
        filmProductRepository.removeById(filmProduct.getId());
    }

    @Override
    public FilmProduct filmProductInfo(Long productId) {
        if (ObjectUtil.isNull(productId)) {
            throw new BusinessException("产品信息不存在");
        }
        return filmProductRepository.getById(productId);
    }
}
